package com.simple.gateway.center.basedata.repository;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.simple.gateway.center.basedata.common.SimpleGatewayConstants;
import com.simple.gateway.center.basedata.mapper.GatewayUserMapper;
import com.simple.gateway.center.basedata.mapper.enhance.GatewayUserInterfaceEnhanceMapper;
import com.simple.gateway.center.basedata.po.GatewayUser;
import com.simple.gateway.center.basedata.po.GatewayUserInterfaceRelation;
import com.simple.gateway.center.domian.auth.bo.GatewayUserBO;
import com.simple.gateway.center.domian.auth.param.BindInterfacesParam;
import com.simple.gateway.center.domian.auth.param.ChangeAuthParam;
import com.simple.gateway.center.domian.auth.param.GatewayUserFullParam;
import com.simple.gateway.center.domian.auth.repository.IAuthRepository;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 项目: simple-gateway-core
 *
 * 功能描述: 授权
 *
 * @author: WuChengXing
 *
 * @create: 2023-08-29 22:43
 **/
@Component
public class AuthRepository implements IAuthRepository {

    @Resource
    private GatewayUserMapper gatewayUserMapper;

    @Resource
    private GatewayUserInterfaceEnhanceMapper enhanceMapper;

    @Override
    public Boolean registerUser(GatewayUserBO gatewayUserBO) {
        gatewayUserMapper.insertGatewayUser(GatewayUserBO.bo2Do(gatewayUserBO));
        return true;
    }

    @Override
    public Boolean bindInterfaces(BindInterfacesParam bindInterfacesParam) {
        Long gatewayUserId = bindInterfacesParam.getGatewayUserId();
        List<Long> interfaceMethodIds = bindInterfacesParam.getInterfaceMethodIds();
        List<GatewayUserInterfaceRelation> saveData = new ArrayList<>();
        for (Long interfaceMethodId : interfaceMethodIds) {
            GatewayUserInterfaceRelation relation = new GatewayUserInterfaceRelation();
            relation.setGatewayUserId(gatewayUserId);
            relation.setApplicationInterfaceMethodId(interfaceMethodId);
            saveData.add(relation);
        }
        return enhanceMapper.saveBatch(saveData);
    }

    @Override
    public Boolean changeAuthInto(ChangeAuthParam changeAuthParam) {
        LambdaQueryWrapper<GatewayUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GatewayUser::getIsDeleted, SimpleGatewayConstants.NOT_DELETED);
        queryWrapper.eq(StrUtil.isNotBlank(changeAuthParam.getUserId()), GatewayUser::getUserId, changeAuthParam.getUserId());
        queryWrapper.eq(Objects.nonNull(changeAuthParam.getId()), GatewayUser::getId, changeAuthParam.getId());
        GatewayUser gatewayUser = new GatewayUser();
        gatewayUser.setAuthType(changeAuthParam.getAuthType());
        return gatewayUserMapper.update(gatewayUser, queryWrapper) > 0;
    }

    @Override
    public List<GatewayUserBO> listGateUser(GatewayUserFullParam param) {
        LambdaQueryWrapper<GatewayUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GatewayUser::getIsDeleted, SimpleGatewayConstants.NOT_DELETED);
        queryWrapper.eq(StrUtil.isNotBlank(param.getUserId()), GatewayUser::getUserId, param.getUserId());
        queryWrapper.like(StrUtil.isNotBlank(param.getUserName()), GatewayUser::getUserName, param.getUserName());
        queryWrapper.eq(StrUtil.isNotBlank(param.getAuthType()), GatewayUser::getAuthType, param.getAuthType());
        List<GatewayUser> gatewayUsers = gatewayUserMapper.selectList(queryWrapper);
        return gatewayUsers.stream().map(GatewayUserBO::do2Bo).collect(Collectors.toList());
    }

    @Override
    public Boolean hasUserInSystem(String userKey) {
        GatewayUserFullParam gatewayUserFullParam = new GatewayUserFullParam();
        gatewayUserFullParam.setUserId(userKey);
        List<GatewayUserBO> gatewayUserBOS = this.listGateUser(gatewayUserFullParam);
        return CollectionUtil.isNotEmpty(gatewayUserBOS);
    }
}
